﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using Labthink.PCSoft.UniversalHost4.Utility;
using SystemSettings;
using Labthink.PCSoft.UniversalHost4.DataAccess;
using Labthink.PCSoft.UniversalHost4.Model.Setting;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcSecurity
    {
        private NotifyManager _notify;

        public string Title { get; set; } = LanguageManager.GetLanguageValue("SafeInfoSetting");

        public string PermissionCode { get; set; } = "Per_SecuritySetting";

        public UcSecurity()
        {
            InitializeComponent();
            BtnOk.Focus();
        }

        private void UcSecurity_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            var w = Window.GetWindow(this);
            _notify = NotifyManager.InstanceManager(w);

            LoadSafeInfo();
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            _triggerButtonsStatus(false);

            var safeInfo = new SafeInfo();
            safeInfo.DefaultPassword = TbDefaultPassword.Value;
            safeInfo.Complexity = string.IsNullOrEmpty(TbPasswordComplexity.Value) ? 3 : TbPasswordComplexity.Value.ToInt();
            safeInfo.PwdMinLength = string.IsNullOrEmpty(TbPasswordMinLength.Value) ? 6 : TbPasswordMinLength.Value.ToInt();
            safeInfo.Expires = string.IsNullOrEmpty(TbPasswordValidity.Value) ? 3 : TbPasswordValidity.Value.ToInt();
            safeInfo.LockCount = string.IsNullOrEmpty(TbPasswordRetry.Value) ? 5 : TbPasswordRetry.Value.ToInt();
            safeInfo.LockTime = string.IsNullOrEmpty(TbLockValidity.Value) ? 30 : TbLockValidity.Value.ToInt();
            safeInfo.ResetTime = string.IsNullOrEmpty(TbLockResetInterval.Value) ? 5 : TbLockResetInterval.Value.ToInt();
            safeInfo.AccountLockTime = string.IsNullOrEmpty(TbAccountLock.Value) ? 0 : TbAccountLock.Value.ToInt();
            safeInfo.PasswordHistory = string.IsNullOrEmpty(TbPasswordHistory.Value) ? 0 : TbPasswordHistory.Value.ToInt();

            try
            {
                ConfigAndSettings.SetSafeInfo(safeInfo);
                Common.SafeInfo = safeInfo;
                LogRepository.AddLog(LanguageManager.GetLanguageValue("OP-SetSafeInfo"), "Manage");
                _notify.ShowSuccess(LanguageManager.GetLanguageValue("ActionSuccess"));
            }
            catch (Exception ex)
            {
                _notify.ShowError(ex.Message);
            }

            _triggerButtonsStatus(true);
        }

        private void LoadSafeInfo()
        {
            _triggerButtonsStatus(false);

            SafeInfo safeInfo = Common.SafeInfo;
            TbDefaultPassword.Value = safeInfo.DefaultPassword;
            TbPasswordComplexity.Value = safeInfo.Complexity+"";
            TbPasswordMinLength.Value = safeInfo.PwdMinLength + "";
            TbPasswordValidity.Value = safeInfo.Expires + "";
            TbPasswordRetry.Value = safeInfo.LockCount + "";
            TbLockValidity.Value = safeInfo.LockTime + "";
            TbLockResetInterval.Value = safeInfo.ResetTime + "";
            TbAccountLock.Value = safeInfo.AccountLockTime + "";
            TbPasswordHistory.Value = safeInfo.PasswordHistory + "";

            if (Common.BaseInfo.Gmp)
            {
                TbPasswordMinLength.IsEnabled = false;
                TbPasswordValidity.IsEnabled = false;
                TbPasswordRetry.IsEnabled = false;
                TbLockValidity.IsEnabled = false;
                TbLockResetInterval.IsEnabled = false;
                TbPasswordComplexity.MinValue = 3;
                TbPasswordMinLength.Value = 8 + "";
                TbPasswordValidity.Value = 3 + "";
                TbPasswordRetry.Value = 5 + "";
                TbLockValidity.Value = 0 + "";
                TbLockResetInterval.Value = 5 + "";
                TbAccountLock.Value = "20";
            }

            _triggerButtonsStatus(true);
        }

        private void _triggerButtonsStatus(bool status)
        {
            BtnOk.IsEnabled = status;
            Loading.Visibility = status ? Visibility.Collapsed : Visibility.Visible;
        }
    }
}
